package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.jwgf.adapter.MultiItemTypeAdapter;
import com.jwgf.itemviewdelegates.ImageDelegate;
import com.jwgf.objectbox.ObjectBox;
import com.jwgf.objectbox.ShiftReport;
import com.jwgf.objectbox.ShiftReport_;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import io.objectbox.Box;

public class QRCodeGalleryDialog extends DialogFragment {
    private MultiItemTypeAdapter<Long> adapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gallery_layout, container);
        ViewPager2 viewPager = view.findViewById(R.id.view_pager);
        TextView tv = view.findViewById(R.id.title);
        Box<ShiftReport> box = ObjectBox.get().boxFor(ShiftReport.class);
        long[] times = box.query().build().property(ShiftReport_.start).distinct().findLongs();
        List<Long> timeList = new ArrayList<>();
        for (long time : times) {
            timeList.add(time);
        }
        Collections.sort(timeList);
        adapter = new MultiItemTypeAdapter<Long>(getContext(), timeList);
        adapter.addItemViewDelegate(new ImageDelegate());
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(adapter.getDatas().get(position));
                StringBuilder sb = new StringBuilder();
                sb.append(c.get(Calendar.YEAR)).append("-");
                sb.append(c.get(Calendar.MONTH) + 1).append("-");
                sb.append(c.get(Calendar.DAY_OF_MONTH)).append(" ");
                sb.append(c.get(Calendar.HOUR_OF_DAY)).append(":");
                sb.append(c.get(Calendar.MINUTE)).append("");
                tv.setText(sb.toString());
            }
        });
        viewPager.setOffscreenPageLimit(2);// make sure left/right item is rendered
        viewPager.setClipChildren(false);// allow left/right item is not clipped
        viewPager.setClipToPadding(false);// allow full width shown with padding
        // increase this offset to show more of left/right
        viewPager.setPadding(100, 0, 100, 0);
        // increase this offset to increase distance between 2 items
        viewPager.setPageTransformer(new MarginPageTransformer(10));
        viewPager.setAdapter(adapter);
        return view;
    }
}
